package net.fancy.main;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.fancy.util.ClockAdapter;
import net.fancy.util.Constants;
import net.fancy.util.ListenerAdapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

/**
 * @author fancylou
 *
 */
public class FancyClock extends Activity {
	/** Called when the activity is first created. */

	private Button addbtn;
	static final int REQUEST_CODE = 1;
	public static FancyClock fc;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		fc = this;

		ListView listview = (ListView) findViewById(R.id.alarmsList);
		addbtn = (Button) findViewById(R.id.addAlarm);

		// 生成动态数组，加入数据
		List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < 8; i++) {
			HashMap<String, Object> hm = new HashMap<String, Object>();
			hm.put("ItemTitle", "标题" + i);
			hm.put("ItemContent", "内容是什么是");
			list.add(hm);
		}
		// 生成适配器的Item和动态数组对应的元素
//		ListenerAdapter sadapter = new ListenerAdapter(this, list,
//				R.layout.itemlist,// 数据源 //ListItem的XML实现
//				new String[] { "ItemTitle", "ItemContent" },// 动态数组与ImageItem对应的子项
//				new int[] { R.id.ItemTitle, R.id.ItemContent });
		ClockAdapter sadapter = new ClockAdapter(this,list,R.layout.itemlist);
		// 添加并且显示
		listview.setAdapter(sadapter);

		// 添加点击事件
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				setTitle("点击第" + arg2 + "个项目");
			}

		});

		// 添加button点击事件
		addbtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				/**
				 * 生成对话框 选择新增类型 进入不同页面
				 */
				new AlertDialog.Builder(FancyClock.this).setIcon(
						android.R.drawable.ic_dialog_info).setTitle("请选择类型")
						.setSingleChoiceItems(new String[] { "闹钟功能", "提醒功能" },
								0, new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface arg0,
											int arg1) {
										// TODO Auto-generated method stub
										Log.d(Constants.FANCY_LOG, "选择类型："
												+ arg1);
										arg0.dismiss();
										if (arg1 == 0) {// 闹钟
											Intent intentalarm = new Intent(
													FancyClock.this,
													AlarmActivity.class);
											intentalarm.putExtra("ItemType",
													"数据来自FancyClock" + arg1);
											startActivityForResult(intentalarm,
													REQUEST_CODE);
										} else {
											Intent intentremind = new Intent(FancyClock.this,RemindActivity.class);
											intentremind.putExtra("ItemType",
													"数据来自FancyClock" + arg1);
											startActivityForResult(intentremind,
													REQUEST_CODE);

										}

									}

								}).setNegativeButton("取消", null).show();
			}
		});

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		Log.d(Constants.FANCY_LOG, "requestCode:" + requestCode
				+ " resultCode:" + resultCode);
		if(resultCode == RESULT_OK){
			Bundle extras = data.getExtras();
			String back = extras.getString("store");
			Log.d(Constants.FANCY_LOG, back);
		}
	}

}